package rs.etf.kdp.client;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * Sends list of components, list of connections, and type to server
 * @author Djordje Vukovic
 * @version 1.0
 * @created 16-May-2011 4:59:08 PM
 */
public class Sender extends Thread{

	private Socket server;
	private String serverHost;
	private int serverPort;
	private ClientContext context;
	boolean loop = true;
	private Job currentJob;
	private long fromServerId;
	private GeneralJobsManager manager;
	private File res;
	private RequestedJobsCollection req;
	private RegistrationBuffer buff;
	public static List<FileLocation> fileLocations = new ArrayList<FileLocation>();

	ObjectInputStream serverIn;
	ObjectOutputStream serverOut;
	
	public Sender(ClientContext c, Job j, GeneralJobsManager gjm, File result, RequestedJobsCollection request, ObjectOutputStream output, ObjectInputStream input, RegistrationBuffer buff){
		req = request;
		res = result;
		context = c;
		serverHost = context.getAddress();
		serverPort = context.getPort();
		currentJob = j;
		manager = gjm;
		serverIn = input;
		serverOut = output;
		this.buff = buff;
	}

	/**
	 * 
	 * @param currentJob
	 */
	public void sendJob(Job currentJob){
		//String command = "client " + "newjob " + currentJob.getIdentifier();
		String command = "new job";
		String answer = null;
		try {
			serverOut.writeObject(command);
		} catch (IOException e) {
			context.log.append("\nSender: Error while sending newjob message ");
			return;
		}
		
//		try {
//			answer = (String)serverIn.readObject();
//		} catch (IOException e) {
//			context.log.append("\nSender: Error while recieving sendnewjob message ");
//			return;
//		} catch (ClassNotFoundException e) {
//			context.log.append("\nSender: ClassNotFoundException: cast error ");
//			return;
//		}
		long ans = buff.getID();
		answer = new Long(ans).toString();
		
		if(Long.parseLong(answer)>=0)
		{
			fromServerId = Long.parseLong(answer);
			Sender.FileLocation newFile = new Sender.FileLocation(fromServerId, res);
			Sender.fileLocations.add(newFile);
			req.addNewJob(new RememberedJob(fromServerId, ClientGUI.nameOfThisClient, "SENT"));
			currentJob.setServerID(fromServerId);
			try {
				serverOut.writeObject(currentJob);
			} catch (IOException e) {
				context.log.append("\nSender: Error while sending job object ");
				return;
			}
		}
		
//		String getJobConfirmation = null;
//		try
//		{
//			getJobConfirmation = (String)serverIn.readObject();
//		} catch(IOException ioe)
//		{
//			context.log.append("\nSender: Error while recieving job confirmation message ");
//			return;
//		} catch (ClassNotFoundException e) {
//			context.log.append("\nSender: ClassNotFoundException: cast error while recieving job confirmation message ");
//			return;
//		}
//		
//		if(getJobConfirmation.equals("jobrecieved"))
//		{
//			context.log.append("\nSender: Job is successfully sent to the server ");
//		}
			
		
	}
	
	public void sendAbortMessage(long jobId)
	{
		String message = "client abort " + jobId;
		try {
			serverOut.writeObject(message);
		} catch (IOException e) {
			context.log.append("\nUnable to send abort message");
		}
	}
	
	public void run()
	{
		
		sendJob(currentJob);
//		if(ClientGUI.rec==null)
//		{
//			ClientGUI.rec= new Receiver(serverIn, context.log);
//			ClientGUI.rec.start();
//		}
		manager.insert(fromServerId, context.getIdentifier(), this, "Ready", context.getFresults());
		context.list.addElement(fromServerId);
		
	}

	public Socket getServer() {
		return server;
	}

	public void setServer(Socket server) {
		this.server = server;
	}

	public String getServerHost() {
		return serverHost;
	}

	public void setServerHost(String serverHost) {
		this.serverHost = serverHost;
	}

	public int getServerPort() {
		return serverPort;
	}

	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	public ClientContext getContext() {
		return context;
	}

	public void setContext(ClientContext context) {
		this.context = context;
	}

	public ObjectInputStream getServerIn() {
		return serverIn;
	}

	public void setServerIn(ObjectInputStream serverIn) {
		this.serverIn = serverIn;
	}

	public ObjectOutputStream getServerOut() {
		return serverOut;
	}

	public void setServerOut(ObjectOutputStream serverOut) {
		this.serverOut = serverOut;
	}

	public boolean isLoop() {
		return loop;
	}

	public void setLoop(boolean loop) {
		this.loop = loop;
	}

	public long getFromServerId() {
		return fromServerId;
	}

	public void setFromServerId(long fromServerId) {
		this.fromServerId = fromServerId;
	}

	public GeneralJobsManager getManager() {
		return manager;
	}

	public void setManager(GeneralJobsManager manager) {
		this.manager = manager;
	}
	
	
	public static class FileLocation
	{
		long id;
		File file;
		
		public FileLocation(long id, File file) {
			super();
			this.id = id;
			this.file = file;
		}

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public File getFile() {
			return file;
		}

		public void setFile(File file) {
			this.file = file;
		}
		
		
	}
	

}